import Util from '../../utils/util.js';
import {bind_phone} from "../../api/api";

const app = getApp()
Component({
  properties: {
    iosDialog: {
      type: Boolean,
      value: false
    },

    showShareDialog: {
      type: Boolean,
      value: false
    },

    accountDialog: {
      type: Boolean,
      value: false
    },

    chargeAccountDialog: { //充值弹窗
      type: Boolean,
      value: false
    },

    from: {
      type: String,
      value: ""
    },
    dialog: {
      type: Object,
    },
    isClick: Boolean,
    dialogType: {
      type: String,
      observer: function (newVal, index, changePath) {
        const that = this;
        if (newVal == 'service_subscribe' || newVal == 'service_subscribe_click') {

          if (newVal == 'service_subscribe') {
            const current_timestamp = Date.parse(new Date())
            let close_service_dialog_time = wx.getStorageSync("close_service_dialog_time");
            if (close_service_dialog_time + 0.5 * 3600 * 1000 > current_timestamp) {
              this.setData({
                dialogType: ""
              })
              return
            }
          }
        }
      }
    }
  },
  data: {},
  attached: function () {

    const that = this;
    Util.isIphone().then(res => {
      that.setData({isPhone: res})
    });

  },
  methods: {
    close(e) {
      this.setData({
        iosDialog: false,
        showShareDialog: false,
        accountDialog: false,
        dialogType: ""
      })
      if (this.data.dialogType == 'service_subscribe') {
        wx.setStorageSync('close_service_dialog_time', Date.parse(new Date()));
      }
      this.triggerEvent("closeDialog")
    },

    toZnws() { //跳转到中南外送
      wx.navigateToMiniProgram({
        appId: 'wxf01be12354b159d2',
        path: 'pages/index/index',
        success(res) {
        },
      })
    },

    callPhoneNumberClose(e) {
      this.setData({
        iosDialog: false,
        showShareDialog: false,
        accountDialog: false,
      })
      this.triggerEvent("dialogCancel", {type: 99});
    },

    getPhoneNumber(e) {
      const that = this;

      if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
        return app.Tips({
          title: "取消授权",
          icon: "error",
          hideMaskClose: true
        })
      } else if (e.detail.errMsg == 'getPhoneNumber:ok') {
        let param = e.detail;
        bind_phone(param).then(res => {
          wx.setStorageSync('is_refresh', 1)
          this.triggerEvent("dialogConfirm", {type: 99, mobile: res.data.mobile || ''});

          that.close()
          return app.Tips({
            title: "验证成功",
            icon: "success",
            hideMaskClose: true
          })
        }).catch(errmsg => {
          that.close()
          return app.Tips({title: errmsg})
        })
      }
    },

    handleAgreePrivacyAuthorization(e) {
      console.log(e)
    },

  }
});
